package com.dlc.beijingrfid.net.protocol;

import android.text.TextUtils;
import com.dlc.beijingrfid.net.bean.BackBean;
import com.dlc.beijingrfid.net.bean.OrderCheckBean;
import com.dlc.beijingrfid.net.bean.OrderDetailBean;
import com.dlc.beijingrfid.net.bean.OrderRfidsBean;
import com.dlc.beijingrfid.net.bean.OrderWashDetailBean;
import com.dlc.beijingrfid.net.bean.RFIDListBean;
import com.dlc.beijingrfid.net.bean.RFIDSpecListBean;
import com.dlc.beijingrfid.net.bean.SimpleBean;
import com.dlc.beijingrfid.net.bean.WashBean;
import com.dlc.beijingrfid.net.protocol.base.HttpProtocol;
import com.dlc.commonlibrary.utils.RxUtil;
import com.lzy.okgo.model.HttpParams;
import rx.Observable;
import rx.functions.Func1;

/**
 * 用来复制粘贴做模板的类
 */
public class MyProto extends HttpProtocol {

    private MyProto() {
    }

    /**
     * RFID列表builder类
     */
    private class HttpParamsBuilder {
        private String category_id;
        private String spec_id;
        private String goods_id;
        private String ids;
        private String warehouse_id;
        private String ware_frame_id;
        private String rfids;
        private String page;
        private String pagesize;

        public HttpParams buid() {
            HttpParams httpParams = new HttpParams();
            if (!TextUtils.isEmpty(category_id)) httpParams.put("category_id", category_id);
            if (!TextUtils.isEmpty(spec_id)) httpParams.put("spec_id", spec_id);
            if (!TextUtils.isEmpty(goods_id)) httpParams.put("goods_id", goods_id);
            if (!TextUtils.isEmpty(ids)) httpParams.put("ids", ids);
            if (!TextUtils.isEmpty(warehouse_id)) httpParams.put("warehouse_id", warehouse_id);
            if (!TextUtils.isEmpty(ware_frame_id)) httpParams.put("ware_frame_id", ware_frame_id);
            if (!TextUtils.isEmpty(rfids)) httpParams.put("rfids", rfids);
            if (!TextUtils.isEmpty(page)) httpParams.put("page", page);
            if (!TextUtils.isEmpty(pagesize)) httpParams.put("pagesize", pagesize);
            if (httpParams.urlParamsMap.isEmpty()) {
                throw new NullPointerException("HttpParams's size can't be null");
            }
            return httpParams;
        }

        public HttpParamsBuilder addCategory_id(String category_id) {
            this.category_id = category_id;
            return this;
        }

        public HttpParamsBuilder addSpec_id(String spec_id) {
            this.spec_id = spec_id;
            return this;
        }

        public HttpParamsBuilder addGoods_id(String goods_id) {
            this.goods_id = goods_id;
            return this;
        }

        public HttpParamsBuilder addIds(String ids) {
            this.ids = ids;
            return this;
        }

        public HttpParamsBuilder addWarehouse_id(String warehouse_id) {
            this.warehouse_id = warehouse_id;
            return this;
        }

        public HttpParamsBuilder addWare_frame_id(String ware_frame_id) {
            this.ware_frame_id = ware_frame_id;
            return this;
        }

        public HttpParamsBuilder addRfids(String rfids) {
            this.rfids = rfids;
            return this;
        }

        public HttpParamsBuilder addPage(String page) {
            this.page = page;
            return this;
        }

        public HttpParamsBuilder addPagesize(String pagesize) {
            this.pagesize = pagesize;
            return this;
        }
    }

    /**
     * 获取RFID列表
     *
     * @param rfid
     * @return
     */
    public Observable<RFIDListBean.DataBean> query_by_rfid(String rfid,String page,String pagesize) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("rfids", rfid);
        httpParams.put("page", page);
        httpParams.put("pagesize", pagesize);
        return rxPost("rfid", httpParams, RFIDListBean.class, true, null).map(
            new Func1<RFIDListBean, RFIDListBean.DataBean>() {
                @Override
                public RFIDListBean.DataBean call(RFIDListBean rfidListBean) {
                    return rfidListBean.data;
                }
            }).compose(RxUtil.<RFIDListBean.DataBean>rxSchedulerHelper());
    }

    /**
     * RFID分类
     *
     * @param rfid
     * @return
     */
    public Observable<RFIDSpecListBean> rfid_spec(String rfid) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("rfids", rfid);
        return  rxPost("rfid_spec", httpParams, RFIDSpecListBean.class, true, null);
    }

    private static class InstanceHolder {
        private static final MyProto sProtocol = new MyProto();
    }

    public static MyProto get() {
        return InstanceHolder.sProtocol;
    }

    /**
     * 通过orderId(租赁单ID)获取送货单详情
     *
     * @param
     * @return
     */
    public Observable<OrderDetailBean.DataBean> orderDetailById(String id) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("id", id);
        return rxPost("order_deliver_detail", httpParams, OrderDetailBean.class, true, null).map(
            new Func1<OrderDetailBean, OrderDetailBean.DataBean>() {
                @Override
                public OrderDetailBean.DataBean call(OrderDetailBean bean) {
                    return bean.data;
                }
            }).compose(RxUtil.<OrderDetailBean.DataBean>rxSchedulerHelper());
    }

    /**
     * 通过orderno(租赁单号)获取送货单详情
     *
     * @param
     * @return
     */
    public Observable<OrderDetailBean.DataBean> OrderDetailByOrderNo(String orderno) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("orderno", orderno);
        return rxPost("order_deliver_detail", httpParams, OrderDetailBean.class, true, null).map(
            new Func1<OrderDetailBean, OrderDetailBean.DataBean>() {
                @Override
                public OrderDetailBean.DataBean call(OrderDetailBean bean) {
                    return bean.data;
                }
            }).compose(RxUtil.<OrderDetailBean.DataBean>rxSchedulerHelper());
    }

    /**
     * 新送洗订单
     *
     * @param
     * @return
     */
    public Observable<WashBean> order_wash_new(String rfids) {
        HttpParams httpParams = new HttpParams();
        //httpParams.put("id", id);

        httpParams.put("rfids", rfids);

        return rxPost("order_wash_new", httpParams, WashBean.class, true, null);
    }

    /**
     * 入库
     *
     * @param orderno
     * @param diff
     * @param rfids
     * @return
     */
    public Observable<SimpleBean> ruku(String orderno, int diff, String... rfids) {
        HttpParams httpParams = new HttpParams();
        //httpParams.put("id", id);
        httpParams.put("orderno", orderno);
        httpParams.put("diff", diff);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rfids.length; i++) {

            sb.append(rfids[i]);

            if (i != rfids.length - 1) {
                sb.append(',');
            }
        }
        httpParams.put("diff_rfids", sb.toString());

        return rxPost("order_deliver_store", httpParams, SimpleBean.class, true, null);
    }
    public Observable<OrderRfidsBean> order_rfids(String read_rfids, String orderno) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("rfids", read_rfids);
        httpParams.put("orderno", orderno);
        return rxPost("order_rfids", httpParams, OrderRfidsBean.class, true, null);
    }
    /**
     * 出库
     * @param orderno
     * @return
     */
    public Observable<SimpleBean> chuku(String orderno) {
        HttpParams httpParams = new HttpParams();
        //httpParams.put("id", id);
        httpParams.put("orderno", orderno);
        return rxPost("order_deliver_receive", httpParams, SimpleBean.class, true, null);
    }




    /**
     * 送洗单详情,布草交接界面的数据
     *
     * @param
     * @return
     */
    public Observable<OrderWashDetailBean> OrderDetailOrderWashDetail(String orderno) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("orderno", orderno);
        return rxPost("order_wash_detail", httpParams, OrderWashDetailBean.class, true, null);
    }

    /**
     * 送货单详情,布草交接界面的数据
     * @param orderno
     * @return
     */
    public Observable<OrderWashDetailBean> OrderDetailOrderDeliveryDetail(String orderno) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("orderno", orderno);
        return rxPost("order_deliver_detail", httpParams, OrderWashDetailBean.class, true, null);
    }


    /**
     * 送货交接
     * @param orderno
     * @return
     */
    public Observable<BackBean> orderDeliverReceive(String orderno,String rfids) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("orderno", orderno);
        httpParams.put("rfids", rfids);
        return rxPost("order_deliver_receive", httpParams, BackBean.class, true, null);
    }

    /**
     * 送洗交接
     * @param orderno
     * @return
     */
    public Observable<SimpleBean> orderWashReceive(String orderno,String rfids) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("orderno", orderno);
        httpParams.put("rfids", rfids);
        return rxPost("order_wash_receive", httpParams, SimpleBean.class, true, null);
    }

    public Observable<OrderCheckBean> rfid_compare(String read_rfids, String rfids) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("read_rfids", read_rfids);
        httpParams.put("rfids", rfids);
        return rxPost("rfid_compare", httpParams, OrderCheckBean.class, true, null);
    }
}
